﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U.UI
{
    public partial class BoardAddEdit : System.Web.UI.Page
    {
        private int BoardID = -1;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            // lay ArticleID khi chinh sua
            BoardID = Globals.GetIntFromQueryString("BoardID");
            if (Request.QueryString["Action"] == "Edit")
            {
                btSave.CommandArgument = "Edit";
                btSave.Text = " Update ";

                if (!IsPostBack)
                {
                    Load_GetItemByBoardID(BoardID);
                }
            }
        }

        protected void Load_GetItemByBoardID(int BoardID)
        {
            webapp4U.BOL.Board obj = webapp4U.BOL.Board.GetBoardByBoardID(BoardID);
            txtInformation.Text = obj.Information;
            
            if(obj.ImageURL.Length>3)
                ImageItem.Src = Globals.ApplicationPath + "/UserUpload/Board/" + obj.ImageURL;           

            HiddenImageName.Value = obj.ImageURL;
            
        }


        protected void btSave_Click(object sender, EventArgs e)
        {
            webapp4U.BOL.Board obj = new webapp4U.BOL.Board();
            obj.BoardID = BoardID;
            obj.Information = txtInformation.Text;            
            obj.ImageURL = HiddenImageName.Value.Trim();
            
            if (btSave.CommandArgument == "Edit")
            {
                webapp4U.BOL.Board.Update(obj);
                Response.Redirect("Default.aspx");
            }
            else
            {
                webapp4U.BOL.Board.Insert(obj);
                
                Response.Redirect("Default.aspx");

                //txtBody.Text = "";
                //txtTitle.Text = "";
                //txtExcerpt.Text = "";
            }

            lblMsg.Text = "Cập nhật thông tin thành công!";

        }
    }
}